home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / emac16ds.zip / EXECUTE.ASM < prev    next >
Assembly Source File  |  1991-06-13  |  11KB  |  493 lines

  1. ;History:134,1
  2. ;Sun Mar 26 20:54:16 1989 remove reference to textseg.
  3. ;03-27-88 14:27:20 remove execute_filter code.
  4. ;03-13-88 12:26:39 remove the execute filter stuff.
  5. stderr        equ    1
  6.  
  7.     .xlist
  8.     include    memory.def
  9.  
  10. data    segment    byte public
  11.  
  12.     extrn    phd_seg: word
  13.  
  14. fcb_struc    struc
  15. fcb_drive    db    0
  16. fcb_fname    db    '        '
  17. fcb_ext        db    '   '
  18. fcb_curblk    dw    0
  19. fcb_recsz    dw    0
  20. fcb_filsz    dw    0,0
  21. fcb_date    dw    0
  22. fcb_time    dw    0
  23. fcb_res        dd    ?,?
  24. fcb_currec    db    0
  25. fcb_ranrec    dw    0,0
  26. fcb_struc    ends
  27.  
  28. segofs    struc
  29. offs    dw    ?
  30. segm    dw    ?
  31. segofs    ends
  32.  
  33. emmp    struc            ; xms extended memory manager parms
  34. bcountl    dw    ?        ; byte count
  35. bcounth dw    ?
  36. shandle    dw    ?        ; emm handle (source)
  37. soffset    dd    ?        ; offset into source block
  38. dhandle    dw    ?        ; emm handle (destination)
  39. doffset    dd    ?        ; offset into destination block
  40. emmp    ends
  41.  
  42. echo_flag    dw    ?    ;echo STDOUT/STDERR output during spawn
  43. reload_routine    dd    ?    ;pointer to reload routine in phd_seg
  44. swap_sizel    dw    ?    ;temporary save area for swap data size
  45. swap_sizeh    dw    ?
  46. emm_parm    emmp    <>    ;emm parameter block
  47. xms        dd    ?    ;emm driver entry point
  48. block_psize    dw    ?
  49.  
  50. data    ends
  51.  
  52. fn_size equ    64        ;size of file name to save in reload data
  53.  
  54. reload    segment    at 0        ;the order of the data in this segment is
  55. r_base    label    byte        ;critically dependent on the format of
  56.                 ;corresponding data in data and code segments
  57.                 ;modify with caution.
  58.     org    100h
  59. r_ss    dw    ?        ;stack segment
  60. r_sp    dw    ?        ;stack pointer
  61. r_parms    dw    7 dup (?)    ;exec parameter block
  62. r_emmp    emmp    <>        ;xms parm block
  63. r_xms    segofs    <>        ;emm driver entry point
  64. r_psize    dw    ?        ;allocated memory block size in paragraphs
  65. r_dvec    segofs    <>        ;DOS interrupt vector
  66.     db    300 dup (?)    ;temporary stack area
  67. r_stack    dw    ?        ;top of temporary stack
  68. r_fname db    fn_size dup (?)    ;file name to execute
  69. r_subr    label    byte        ;reloader subroutine
  70. r_size    equ    $-r_ss        ;reloader code and data size
  71. r_offset equ    $-r_base
  72. reload    ends
  73.  
  74. code    segment    byte public
  75. ;all the routines in this segment are entered with ds=data, es=data
  76.     assume    cs:code, ds:data, es:data, ss:data
  77.  
  78. ;the following externs are in 'buffers'
  79.     extrn    compact_buffers: near
  80.     extrn    uncompact_buffers: near
  81.  
  82. ;the following externs are in 'memory'
  83.     extrn    read_mark: near
  84.     extrn    del_to_mark: near
  85.     extrn    insert_string$: near
  86.  
  87. ;the following externs are in 'redisplay'
  88.     extrn    redisplay: near
  89.     extrn    paint_screen: near
  90.  
  91.     extrn    init_entry: near
  92.     extrn    uninit_exit: near
  93.  
  94.     extrn    abort_fatal: near    ;fatal error handler
  95.  
  96. our_sp    dw    ?
  97. our_ss    dw    ?
  98.  
  99. parameters    dw    0
  100.         dw    80h, ?
  101.         dw    5ch, ?
  102.         dw    6ch, ?
  103.  
  104. copy_to_dioa:
  105. ;enter with si, cx->command.
  106. ;exit with cs:phd_dioa set to the string.
  107.     push    es
  108.     mov    es,phd_seg        ;copy the command to phd_dioa+1
  109.     mov    di,80h+1
  110.   if 0
  111.     mov    ax,3700h        ;get the switch char.
  112.     int    21h
  113.     mov    al,dl
  114.     stosb
  115.     mov    al,'C'            ;store -C or /C.
  116.     stosb
  117.   endif
  118.     rep    movsb
  119.     mov    al,CR            ;store the terminating CR.
  120.     stosb
  121.     sub    di,80h+1+1        ;don't count the CR.
  122.     mov    ax,di
  123.     mov    es:[80h],al        ;store the count.
  124.     pop    es            ;restore es.
  125.     ret
  126.  
  127.  
  128.     public    execute_program
  129. execute_program:
  130. ;enter with si, cx->path of filter to execute, di ->null terminated filename.
  131. ;        ax = echo STDOUT/STDERR output flag
  132. ;exit with ax=return result.
  133. ;    mov    echo_flag,ax
  134.     mov    echo_flag,0
  135.     push    di
  136.     call    copy_to_dioa
  137.     pop    di
  138.     call    actually_execute
  139.     ret
  140.  
  141.  
  142. actually_execute:
  143. ;enter with di -> filename, cs:phd_dioa = arguments.
  144.     push    di
  145.     call    uninit_exit
  146.     pop    di
  147.  
  148.     push    ds
  149.     push    es
  150.  
  151.     push    di
  152.     call    compact_buffers        ;make room for the program.
  153.     pop    di
  154.  
  155.     mov    dx,phd_seg        ;subtract off the allocated segment.
  156.     sub    bx,dx
  157.     mov    es,dx            ;get es=allocated segment.
  158.     assume    es:nothing
  159.  
  160.     mov    ah,4ah            ;reduce ourself in size.
  161.     int    21h
  162.  
  163.     mov    our_sp,sp        ;remember our stack.
  164.     mov    our_ss,ss
  165.  
  166.     mov    ds,phd_seg        ;move ds first because we need ds.
  167.     assume    ds:nothing
  168.     mov    es,phd_seg
  169.     assume    es:nothing
  170.  
  171.     mov    dx,di            ;set up to execute the program.
  172.  
  173.     mov    ax,2901h        ;parse fcb1.
  174.     mov    si,81h            ;->phd_sdioa.
  175.     mov    di,5ch            ;->phd_fcb1
  176.     int    21h
  177.  
  178.     mov    ax,2901h        ;parse fcb2.
  179.     mov    di,6ch            ;->phd_fcb2.
  180.     int    21h
  181.  
  182.     push    ss            ;ss:dx -> filename to execute.
  183.     pop    ds
  184.     assume    ds:data
  185.     push    cs
  186.     pop    es
  187.     assume    es:code
  188.  
  189.     call    xms_execute        ;swap out to ems, then execute, if
  190.     jnc    actually_execute_2    ;    possible
  191.                     ;otherwise continue with normal exec
  192.     mov    bx,offset parameters
  193.     mov    ax,phd_seg
  194.     mov    es:[bx]+4,ax        ;use original phd parameters.
  195.     mov    es:[bx]+8,ax
  196.     mov    es:[bx]+12,ax
  197.     mov    ax,4b00h
  198.     int    21h
  199.     jc    actually_execute_1
  200.     xor    ax,ax            ;make sure ax is zero if no errors.
  201. actually_execute_1:
  202.  
  203.     cli                ;get our stack back.
  204.     mov    ss,cs:our_ss
  205.     mov    sp,cs:our_sp
  206.     sti
  207.  
  208. actually_execute_2:
  209.     push    ax
  210.  
  211.     mov    bx,0ffffh        ;now grab all of memory again.
  212.     mov    es,phd_seg
  213.     mov    ah,4ah            ;see how much is available.
  214.     int    21h
  215.     mov    ah,4ah            ;grab all of it.
  216.     int    21h
  217.  
  218.     push    cs            ;reset the fatal error address.
  219.     pop    ds
  220.     mov    dx,offset abort_fatal
  221.     mov    ax,2524h
  222.     int    21h
  223.  
  224.     mov    ax,33h*256+1        ;turn break checking back off.
  225.     mov    dl,0            ;  in case someone turned it on.
  226.     int    21h
  227.  
  228.     pop    ax
  229.  
  230.     pop    es
  231.     pop    ds
  232.     assume    ds:data, es:data
  233.  
  234.     push    ax
  235.     call    uncompact_buffers        ;make room for the text again.
  236.     call    init_entry
  237.     call    paint_screen
  238.     pop    ax
  239.     ret
  240.  
  241.  
  242.     .286
  243.  
  244.     assume    cs:code, ds:data,es:nothing
  245. xms_execute:
  246. ; enter with dx -> file name to execute, bx - number of paragraphs allocated
  247.     mov    ax,4300h    ;if xms driver not installed
  248.     int    2fh        ;     (himem.sys, et.al.)
  249.     cmp    al,80h
  250.     je    xms_execute_1
  251.     stc            ;    return error indication to continue
  252.     ret            ;    exec without swapping
  253. xms_execute_1:
  254.     push    bx
  255.     push    es
  256.     mov    ax,4310h    ;fetch emm driver entry point
  257.     int    2fh
  258.     mov    xms.offs,bx
  259.     mov    xms.segm,es
  260.     pop    es
  261.     pop    bx
  262.  
  263.     mov    block_psize,bx    ;save allocated memory size
  264.     mov    di,dx        ;save file name pointer
  265.     sub    bx,16        ;subtract psp size from paragraph count
  266.     mov    ax,bx        ;compute size of area to be swapped
  267.     mov    dl,ah
  268.     shr    dl,4
  269.     xor    dh,dh
  270.     shl    ax,4
  271.     mov    swap_sizel,ax
  272.     mov    swap_sizeh,dx
  273.     mov    dx,bx        ;allocate xms memory
  274.     add    dx,63        ;round paragraph count up to multiple of 1024
  275.     shr    dx,6
  276.     mov    ah,09h
  277.     call    xms
  278.     cmp    ax,1        ;if error on allocation
  279.     je    xms_execute_2
  280.     stc            ;    abort swap proceedure
  281.     ret
  282. xms_execute_2:                ;endif
  283.                 ;swap out the memory used by reload routine
  284.     mov    emm_parm.bcountl,reload_swap_size
  285.     mov    emm_parm.bcounth,0
  286.     mov    emm_parm.dhandle,dx
  287.     mov    emm_parm.doffset.offs,0
  288.     mov    emm_parm.doffset.segm,0
  289.     mov    emm_parm.shandle,0
  290.     mov    emm_parm.soffset.offs,100h
  291.     mov    es,phd_seg
  292.     assume    es:reload
  293.     mov    emm_parm.soffset.segm,es
  294.     mov    si,offset data:emm_parm
  295.     mov    ah,0bh
  296.     call    xms
  297.     cmp    ax,1        ;if error on swap out
  298.     je    xms_execute_3
  299.     stc            ;    abort swap proceedure
  300.     ret
  301. xms_execute_3:                ;endif
  302.  
  303.                 ;copy needed data to reload data area
  304.     mov    si,di        ;    first the executable file name
  305.     mov    di,offset reload:r_fname
  306.     mov    cx,fn_size
  307.     rep    movsb
  308.  
  309.     push    ds        ;    then the DOS exec block
  310.     mov    ax,cs
  311.     mov    ds,ax
  312.     mov    si,offset code:parameters
  313.     mov    di,offset reload:r_parms
  314.     mov    cx,7
  315.     rep    movsw
  316.     pop    ds        ;    followed by the XMS move parm block
  317.     mov    si,offset data:emm_parm
  318.     mov    cx,11
  319.     rep    movsw
  320.  
  321.                 ;copy reload subroutine
  322.     push    ds
  323.     mov    ds,ax
  324.     mov    di,offset reload:r_subr
  325.     mov    si,offset code:reload_subr
  326.     mov    cx,reload_size
  327.     rep    movsb
  328.     pop    ds
  329.  
  330.                 ;adjust reload xms reload parameters to save
  331.                 ;all but resident subroutine and data
  332.     mov    ax,swap_sizel
  333.     mov    dx,swap_sizeh
  334.     sub    ax,reload_swap_size
  335.     sbb    dx,0
  336.     mov    r_emmp.bcountl,ax
  337.     mov    r_emmp.bcounth,dx
  338.     add    r_emmp.doffset.offs,reload_swap_size
  339.     mov    r_emmp.soffset.offs,reload_swap_size+100h
  340.  
  341.     cmp    echo_flag,0    ;if echo flag set
  342.     je    xms_execute_4
  343.     push    es
  344.     mov    ax,3521h    ;   hook us into the DOS interrupt chain
  345.     int    21h
  346.     mov    ax,es
  347.     pop    es
  348.     mov    word ptr r_dvec,bx
  349.     mov    word ptr r_dvec+2,ax
  350.     mov    ax,2521h
  351.     mov    dx,dv_offset
  352.     push    es
  353.     pop    ds
  354.     int    21h
  355.     push    ss
  356.     pop    ds
  357. xms_execute_4:
  358.  
  359.                 ;call the resident subroutine
  360.     mov    ax,offset reload:r_subr
  361.     mov    reload_routine.offs,ax
  362.     mov    reload_routine.segm,es
  363.     call    reload_routine
  364.     push    ax        ;save exec return code
  365.  
  366.     cmp    echo_flag,0    ;if echo flag set
  367.     je    xms_execute_5
  368.     mov    ax,2521h    ;   remove us from DOS interrupt chain
  369.     lds    dx,r_dvec
  370.     int    21h
  371.     push    ss
  372.     pop    ds
  373. xms_execute_5:
  374.  
  375.                 ;restore code overlaid by resident s & d
  376.     mov    ax,emm_parm.dhandle
  377.     xchg    ax,emm_parm.shandle
  378.     mov    emm_parm.dhandle,ax
  379.     mov    ax,emm_parm.doffset.offs
  380.     xchg    ax,emm_parm.soffset.offs
  381.     mov    emm_parm.doffset.offs,ax
  382.     mov    ax,emm_parm.doffset.segm
  383.     xchg    ax,emm_parm.soffset.segm
  384.     mov    emm_parm.doffset.segm,ax
  385.     mov    si,offset data:emm_parm
  386.     mov    ah,0bh
  387.     call    xms
  388.                 ;release extended memory
  389.     mov    dx,emm_parm.shandle
  390.     mov    ah,0ah
  391.     call    xms
  392.  
  393.     pop    ax        ;restore exec return code
  394.     clc            ;signal sucessful exec and reload
  395.  
  396.     ret
  397.  
  398.     assume    cs:code, ds:data, es:reload
  399.  
  400. reload_subr:            ;resident reload subroutine gets copied to
  401.                 ; low code space while swapped out
  402.     push    ds
  403.     mov    r_sp,sp        ;remember our stack.
  404.     mov    r_ss,ss
  405.  
  406.     mov    bx,cs        ;switch to temporary stack
  407.     mov    ss,bx
  408.     mov    sp,offset reload:r_stack
  409.  
  410.     mov    ds,bx        ;establish addressability to phd segment
  411.     assume    ds:reload
  412.                 ;swap out primary code and data area
  413.     mov    si,offset reload:r_emmp
  414.     mov    ah,0bh
  415.     call    r_xms
  416.  
  417.                 ;free swapped out code and data
  418.     mov    bx,reload_swap_size+100h
  419.     shr    bx,4
  420.     mov    ah,4ah
  421.     int    21h
  422.  
  423.     mov    dx,offset reload:r_fname
  424.     mov    bx,offset reload:r_parms
  425.     mov    [bx]+4,ds    ;use original phd parameters.
  426.     mov    [bx]+8,ds
  427.     mov    [bx]+12,ds
  428.     mov    ax,4b00h
  429.     int    21h
  430.     jc    xms_execute_6
  431.     xor    ax,ax        ;make sure ax is zero if no errors.
  432. xms_execute_6:
  433.     mov    bx,cs        ;restore temporary stack
  434.     mov    ss,bx
  435.     mov    sp,offset reload:r_stack
  436.  
  437.     mov    es,bx        ;restore data segment registers
  438.     mov    ds,bx
  439.  
  440.     mov    bx,r_psize    ;reallocate swapped memory
  441.     mov    ah,4ah
  442.     int    21h
  443.                 ;restore primary swapped out code and data
  444.     mov    ax,r_emmp.dhandle
  445.     xchg    ax,r_emmp.shandle
  446.     mov    r_emmp.dhandle,ax
  447.     mov    ax,r_emmp.doffset.offs
  448.     xchg    ax,r_emmp.soffset.offs
  449.     mov    r_emmp.doffset.offs,ax
  450.     mov    ax,r_emmp.doffset.segm
  451.     xchg    ax,r_emmp.soffset.segm
  452.     mov    r_emmp.doffset.segm,ax
  453.     mov    si,offset reload:r_emmp
  454.     mov    ah,0bh
  455.     call    r_xms
  456.  
  457.     mov    ss,cs:r_ss    ;restore primary stack
  458.     mov    sp,cs:r_sp
  459.  
  460.     pop    ds
  461.     retf
  462.  
  463.     assume    cs:code, ds:nothing, es:nothing
  464. dv_offset   equ    r_offset + $ - reload_subr
  465. dos_vect:
  466.     cmp    ah,40h
  467.     jne    dv_exit
  468.     cmp    bx,2
  469.     jg    dv_exit
  470.     push    si
  471.     push    cx
  472.     push    bx
  473.     push    ax
  474.     mov    si,dx
  475. xms_execute_7:    lodsb
  476. ;    int    29h
  477.     mov    ah,0eh
  478.     int    10h
  479.     loop    xms_execute_7
  480.     pop    ax
  481.     pop    bx
  482.     pop    cx
  483.     pop    si
  484. dv_exit:
  485.     jmp    cs:r_dvec
  486.  
  487. reload_size  equ    $-reload_subr
  488. reload_swap_size equ    (r_size + reload_size + 1) and 0fffeh
  489.  
  490. code    ends
  491.  
  492.     end
  493.